package com.pfzy.utildemo.activity.map;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;

import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.services.core.LatLonPoint;
import com.pfzy.utildemo.R;
import com.pfzy.utildemo.activity.base.BaseActivity;
import com.pfzy.utildemo.activity.mqtt.MQService;
import com.pfzy.utildemo.databinding.ActivityWebMapBinding;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class WebMapActivity extends BaseActivity implements View.OnClickListener {
    private ActivityWebMapBinding mBind;

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        mBind = ActivityWebMapBinding.inflate(getLayoutInflater());
        setContentView(mBind.getRoot());
        initView();
    }

    private void initView() {
        // 设置地图缩略图的中心点和缩放级别
        LatLonPoint point = new LatLonPoint(39.990459, 116.481476); // 北京天安门的位置
        int zoomLevel = 11; // 缩放级别

//        // 生成Web地图的缩略图
//        BitmapDescriptor bitmapDescriptor = AMapUtils.createWebviewSnap(point, zoomLevel);
//
//        // 在ImageView中显示缩略图
//        imageView.setImageBitmap(bitmapDescriptor.getBitmap());
//
////        Bitmap mapIcon = getMapThumbnail();
//        mBind.map.setImageBitmap(mapIcon);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start:
                MQService.actionStart(this);
                break;
            case R.id.stop:
                MQService.actionStop(this);
                break;
            default:
                break;
        }
    }

    public static Bitmap getMapThumbnail(double latitude, double longitude, int zoomLevel) {
        String urlString = "https://maps.googleapis.com/maps/api/staticmap?" +
                "center=" + latitude + "," + longitude +
                "&zoom=" + zoomLevel +
                "&size=800x400" +
                "&key=YOUR_GOOGLE_MAPS_API_KEY";

        try {
            URL url = new URL(urlString);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myImage = BitmapFactory.decodeStream(input);
            return myImage;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
